﻿Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports System.Threading

''' <summary>
''' Interaction logic for MessageLogUserControl.xaml
''' </summary>
Partial Public Class MessageLogUserControl
    Inherits UserControl
    Private settingsEvent As New EventWaitHandle(False, EventResetMode.AutoReset, "C4F-WhileAway-Settings")
    Private glowStoryboardMessage As Storyboard, glowStoryboardName As Storyboard
    Private _messages As New AwayMessageCollection()
    Private currentMessage As New AwayMessage()

    Public Sub New()
        InitializeComponent()
        _messages.LoadFromDisk()
        listBoxMessages.ItemsSource = _messages

        glowStoryboardMessage = DirectCast(Me.Resources("GlowStoryboardMessage"), Storyboard)
        glowStoryboardName = DirectCast(Me.Resources("GlowStoryboardName"), Storyboard)

        stackPanel1.DataContext = currentMessage
    End Sub

    Public ReadOnly Property Messages() As AwayMessageCollection
        Get
            Return _messages
        End Get
    End Property

    Private Sub buttonLeaveMessage_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        If String.IsNullOrEmpty(currentMessage.Sender) Then
            textBoxName.Focus()
            glowStoryboardName.Begin(textBoxName)
            Return
        End If

        If String.IsNullOrEmpty(currentMessage.Message) Then
            textBoxMessage.Focus()
            glowStoryboardMessage.Begin(textBoxMessage)
            Return
        End If

        LeaveMessage()
    End Sub

    Private Sub LeaveMessage()
        currentMessage.[When] = DateTime.Now

        messages.Add(currentMessage)
        messages.Save()

        settingsEvent.Set()

        currentMessage = New AwayMessage()
        stackPanel1.DataContext = currentMessage
    End Sub

    Private Sub UserControl_Unloaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
        settingsEvent.[Set]()
    End Sub
End Class
